package com.zc.web;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zc.entity.Student;
import com.zc.entity.Teacher;
import com.zc.entity.User;
import com.zc.service.IDepartmentService;
import com.zc.service.IMajorService;
import com.zc.service.IStudentService;
import com.zc.service.ITeacherService;
import com.zc.service.IUserService;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @author yuchao
 * adminLogin() 管理员登陆
 * teacherLogin() 教师登陆
 * studentLogin() 学生登陆
 * quitSystem() 退出系统
 * modifyPassword() 修改密码，由于管理员，教师，学生的修改密码相同，所以只用一个controller作为修改。
 *
 * @author yuchao
 * 修改了teacherLogin() 方法，新增了查询教师信息的功能。并且写入session中。
 *
 */

@Controller
public class UserController {

	@Autowired
	private IUserService userService;

	@Autowired
	private ITeacherService teacherService;

	@Autowired
	private IDepartmentService departmentService;

	@Autowired
	private IStudentService studentService;

	@Autowired
	private IMajorService majorService;



	@RequestMapping(value = "login")
	public String login(String userName,String passWord,String captcha,String optionsRadios ,Model model,HttpServletRequest request){
		HttpSession session1 = request.getSession();
		String md5Password = DigestUtils.md5DigestAsHex(passWord.getBytes());

		Object randomString = session1.getAttribute("randomString");//返回指定属性名的属性值
		if(true){
			//教师
			if(optionsRadios.equals("1")){

				User currentUser = userService.login(userName, md5Password);
				//判断数据库是否有该用户存在 ,如果不存在 跳转到登录失败页面
				if("".equals(currentUser)||currentUser==null) {
					model.addAttribute("message", "用户名或密码错误");
					return "error.jsp";
				}
				//判断用户的权限  如果该用户的全选标识为2  就是教师 则跳转到教师页面
				if(currentUser.getPermission()==2) {
					HttpSession session = request.getSession();
					session.setMaxInactiveInterval(3600);
					// 在t_user表的信息
					session.setAttribute("currentUser", currentUser);

					// 完整的teacher信息
					Teacher teacher = teacherService.showInfoByNo(userName);
					int depId = teacher.getDepartmentId();
					String depName = departmentService.getNameById(depId);
					teacher.setDepartmentName(depName);

					session.setAttribute("teacher", teacher);
					//model.addAttribute("userNo", userNo);
					return "redirect:/ teacher/main";
				}else {
					model.addAttribute("message", "当前用户不是教师");//获取老师的信息，返回到前端显示出来
					return "error.jsp";
				}

			}
			//学生
			else if(optionsRadios.equals("2")){
				User currentUser = userService.login(userName, md5Password);
				if("".equals(currentUser)||currentUser==null) {
					model.addAttribute("message", "用户名或密码错误");
					return "error.jsp";
				}
				if(currentUser.getPermission()==1) {
					HttpSession session = request.getSession();
					session.setMaxInactiveInterval(3600);
					// t_user 表中的关于学生的信息
					session.setAttribute("currentUser", currentUser);

					// t_student 表中的信息
					Student student = studentService.getStudentByNO(userName);
					int majorId = student.getMajorId();
					String majorName = majorService.getNameById(majorId);
					student.setMajorName(majorName);

					session.setAttribute("student", student);
					// model.addAttribute("userNo", userNo);

					//查询课题通过结果
					Integer status= studentService.findTheisTile(student.getId());
					model.addAttribute("status",status);
					return "student/main.jsp";
				}else {
					model.addAttribute("message", "当前用户不是学生");
					return "error.jsp";
				}
			}
			//管理员
			else if(optionsRadios.equals("3")){
				User currentUser = userService.login(userName, md5Password);
				if("".equals(currentUser)||currentUser==null) {
					model.addAttribute("message", "用户名或密码错误");
					return "error.jsp";
				}
				if(currentUser.getPermission()==3) {
					// request.getSession().setAttribute("userNo", userNo);
					HttpSession session = request.getSession();
					session.setMaxInactiveInterval(3600);

					session.setAttribute("currentUser", currentUser);
					//model.addAttribute("userNo", userNo);
					return "admin/main.jsp";
				}else {
					model.addAttribute("message", "当前用户不是管理员");
					return "error.jsp";
				}
			}
		}
		model.addAttribute("message", "验证码错误");
		return "error.jsp";
	}


	@RequestMapping(value="/quit")
	public String quitSystem(Model model,HttpServletRequest request) {

		request.getSession().invalidate();

		return "../../index.jsp";
	}


	/**
	 * 修改密码
	 */

	@RequestMapping(value="/modifyPassword",method=RequestMethod.POST)
	public String modifyPassword(Model model,String newPassword1,String currentUserNo) {

		/*System.out.println("新的密码："+newPassword1);
		System.out.println("账户："+currentUserNo);*/
		String md5Password = DigestUtils.md5DigestAsHex(newPassword1.getBytes());

		int num = userService.modifyPassword(currentUserNo, md5Password);
		System.out.println("修改了"+num+"条数据");
		model.addAttribute("num", num);
		return "modifySuccess.jsp";
	}

	@RequestMapping(value = "/captcha", method = RequestMethod.GET)
	@ResponseBody
	public void captcha(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		CaptchaUtil.outputCaptcha(request, response);
	}
	@RequestMapping(value = "/appLogin",method = RequestMethod.POST)
	@ResponseBody
	public User appLogin(String userName,String password){
		User user = userService.login(userName, password);
		return user;
	}
}

